/*
 * DogeFakeScanner.java
 *
 * Created on 15 luglio 2007, 13.00
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package dogeScan;
import java.io.*;

/**
 *
 * @author daniele
 */
public class DogeFakeScanner extends DogeScan {
    
    public DogeFakeScanner() {
        super();
        scannedMime=fakeImageMime;
    }
    
    /** returns data filename (in this case is a file, but in case of a scanner it should return a "random" filename with consistent extension...) */
    public String getDataPath() {
        return fakeImageDataPath;
    }

    /* returns last scanned image */
    public byte[] getLastScannedImage() {
        return scannedImage;
    }

    /** acquire a new page */
    public void scanNewPage() throws IOException {
        super.scanNewPage();
        //in this fake scanner implementation the document has only one page, so the new one overwrites the old one
        scannedImage=acquirePage();
    }
    
    /** acquire again last acquired page */
    public void rescanLastPage() throws IOException{
        //in this fake scanner implementation the document has only one page
        scannedImage=acquirePage();
    }
    
    /** acquire a new page from the scanner */
    protected byte[] acquirePage() throws IOException {
        //create a new InputStream to read data (in this fake implementation data comes from a file in the classpath...)
        InputStream inStream=getClass().getResourceAsStream(fakeImageDataPath);
        //create an OutputStream to which copy the InputStream data
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        int byteRead;
        //copies data from InputStream to OutputStream
        while ((byteRead=inStream.read())!=-1){
            outStream.write(byteRead);
        }
        //converts the OutputStream in a byte[]
        return outStream.toByteArray();
    }
        
    private static final String fakeImageDataPath="/dogeScan/scan.png";
    private static final String fakeImageMime="image/png";
}
